package com.jingdianjichi.subject.domain.handler;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.entity.SubjectRadioBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * ClassName :BriefTypeHandler
 * Package :com.jingdianjichi.subject.domain.handler
 * Desciption :
 *
 * @Author :
 * @Create : 18:28
 * @Version :1.0
 */


@Component
@Slf4j
public class RaidoTypeHandler implements  SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;


    /**
     * 单选题目的插入
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {

        try {
            List<SubjectRadio> subjectRadios = new LinkedList<>();
            subjectInfoBO.getOptionList().forEach(item->{
                SubjectRadio bo = RadioSubjectConverter.INSTANCE.convertOptionsToEntity(item);
                bo.setSubjectId(subjectInfoBO.getId());
                bo.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
                subjectRadios.add(bo);

            });
            subjectRadioService.batchInsert(subjectRadios);
        }
        catch(Exception e){
            log.error("RaidoTypeHandler.add.subjectRadios.{}",e.getMessage(),e);
        }



    }

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADID;
    }


    /**
     * 单项选择查询
     * 注意：每一个选项都是一个类，所以需要拿到的查询结果 >1
     * @param id
     * @return
     */
    @Override
    public SubjectOptionBO querySubjetInfo(Integer id) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(id.longValue());
        List<SubjectRadio> subjectRadios = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOS = RadioSubjectConverter.INSTANCE.convertEntitysToAnswersBO(subjectRadios);
        SubjectOptionBO subjectOptionBOs = new SubjectOptionBO();
        subjectOptionBOs.setSubjetAnswerBoList(subjectAnswerBOS);
        return subjectOptionBOs;
    }
}
